package com.xc.hotelmgr.order.modules.ali.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xc.hotelmgr.order.common.utils.Constant;
import com.xc.hotelmgr.order.common.utils.R;
import com.xc.hotelmgr.order.modules.ali.sdk.FzSdk;
import com.xc.hotelmgr.order.modules.ali.service.DeliGoodsService;
import com.xc.hotelmgr.order.modules.ali.to.common.AliBaseApiRequest;
import com.xc.hotelmgr.order.modules.ali.to.deligoods.request.DeliGoods;
import com.xc.hotelmgr.order.modules.ali.to.deligoods.response.DeliGoodsResponse;
import com.xc.hotelmgr.order.modules.order.entity.DtStoreEntity;
import com.xc.hotelmgr.order.modules.order.service.DtStoreService;
import com.xc.hotelmgr.order.modules.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service("deliGoodsServiceImpl")
@Slf4j
public class DeliGoodsServiceImpl implements DeliGoodsService {
    @Autowired
    private DtStoreService dtStoreService;

    @Autowired
    private OrderService orderService;

    @Override
    public R deliGood(Long orderCode, Constant.OptType optType,String newBuyOrderId) {
        AliBaseApiRequest aliBaseApiRequest = new AliBaseApiRequest();
        DtStoreEntity dtStoreEntity = dtStoreService.sqlSelectByOrderId(orderCode);
        if(dtStoreEntity == null){
            return R.ok();
        }
        aliBaseApiRequest.setAppKey(dtStoreEntity.getAppkey());
        aliBaseApiRequest.setSecret(dtStoreEntity.getSecret());
        aliBaseApiRequest.setSessionKey(dtStoreEntity.getSessionkey());
        aliBaseApiRequest.setMethod("taobao.xhotel.order.update");
        DeliGoods deliGoods = new DeliGoods();
        deliGoods.setTid(orderCode);
        deliGoods.setOpt_type(optType.getValue());
        aliBaseApiRequest.setJsonObject((JSONObject)JSONObject.toJSON(deliGoods));
        String result = "";
        R r = R.ok();
        //更新订单状态
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(newBuyOrderId)) {
            params.put("newBuyOrderId", newBuyOrderId);
        }
        params.put("orderCode",orderCode);
        params.put("buyStatus",Constant.LocalOrdBuyStatus.Purchased.getValue());
        try {
            result = FzSdk.req(aliBaseApiRequest);
            log.info("飞猪发货信息:___"+result);
        } catch (Exception e) {
            result = FzSdk.req(aliBaseApiRequest);
            log.error("飞猪发货错误：{}，订单号：{}",e.toString(),orderCode);
            r = R.error("飞猪发货错误：" + e.toString());
        }
        DeliGoodsResponse deliGoodsResponse = null;
        if(result.contains("xhotel_order_update_response")) {
            deliGoodsResponse = JSONObject.parseObject(result,DeliGoodsResponse.class);
            if(StringUtils.equals(deliGoodsResponse.getXhotel_order_update_response().getResult().toLowerCase(),"success")){
                //更新订单状态
                if(optType == Constant.OptType.Close){
                    params.put("orderStatus",Constant.LocalOrderStatus.Close.getValue());
                }else {
                    params.put("orderStatus", Constant.LocalOrderStatus.Delivered.getValue());
                }
            }else {
                log.error("飞猪发货错误：{}，订单号：{}",result,orderCode);
                r = R.error("飞猪发货错误：" + result);
            }
        }else {
            log.error("飞猪发货错误：{}，订单号：{}",result,orderCode);
            r = R.error("飞猪发货错误：" + result);
        }
        //更新订单状态
        updateOrderStatus(params);
        return r;
    }

    public R updateOrderStatus(Map<String,Object> params){
        Integer cnt = orderService.sqlUpdateOrderStatus(params);
        return R.ok();
    }

}
